import MyTable from '@/components/MyTable';
import CategoryEdit from '@/pages/front/category/CategoryEdit';
import { getResult } from '@/services/sys/attribute';
import {
  editCategoryByDelete,
  findByDataCategoryListPageSelect,
  findByDataCategoryListSelect,
  findByDataCategoryTreeList,
} from '@/services/sys/MenuService';
import { ActionType } from '@ant-design/pro-table/es/typing';
import { Button, message } from 'antd';
import { useRef, useState } from 'react';

const getAttr1 = async (key: string) => {
  let result = await getResult(key, 1);
  return result;
};
const getAttr = async (key: string) => {
  let result = await getResult(key, 0);
  return result;
};
const request = async () => {
  return new Promise<API.SelectItem[]>((resolve, reject) => {
    findByDataCategoryListSelect()
      .then((res) => {
        var selectItems = res.data;
        selectItems.unshift({ label: '下一单', value: 0 });
        resolve(selectItems);
      })
      .catch((e) => {});
  });
};

const request1 = async () => {
  return new Promise<API.SelectItem[]>((resolve, reject) => {
    findByDataCategoryListPageSelect()
      .then((res) => {
        var selectItems = res.data;
        selectItems.unshift({ label: '下一单', value: '' });
        resolve(selectItems);
      })
      .catch((e) => {});
  });
};

const valueEnum = {
  all: { text: '全部', status: 'Default' },
  open: {
    text: '未解决',
    status: 'Error',
    suffixIcon: 'icon-a-Filter21',
  },
  closed: {
    text: '已解决',
    status: 'Success',
  },
  processing: {
    text: '解决中',
    status: 'Processing',
  },
};

type DataItem = {
  name: string;
  state: string;
};
const column = [
  {
    title: '编号',
    dataIndex: 'id',
    key: 'id',
  },
  {
    title: '图标',
    dataIndex: 'image',
    valueType: 'image',
  },
  {
    title: '汉语名称',
    dataIndex: 'name',
    key: 'name',

    width: 'sm',
  },
  {
    title: '维语名称',
    dataIndex: 'nameUg',
    key: 'nameUg',
    width: 'sm',
  },
  {
    title: '排水',
    dataIndex: 'sort',
    width: 'md',
  },
  {
    title: '目标类型',
    key: 'targetType',
    dataIndex: 'targetType',
    valueType: 'select',
    valueEnum: await getResult('advertisingTargetType', 0),
  },
  {
    title: '系统类型',
    dataIndex: 'type',
    valueType: 'select',
    key: 'type',
    valueEnum: await getResult('frontSystemCategoryType', 0),
  },

  {
    title: '地址',
    dataIndex: 'path',
    key: 'path',
  },
  {
    title: '权限字符',
    dataIndex: 'permission',
    key: 'permission',
  },
  {
    title: '标题显示',
    dataIndex: 'flagIcon',
    key: 'flagIcon',
    valueType: 'switch',
  },
  {
    title: '图标显示',
    dataIndex: 'flagTitle',
    key: 'flagTitle',
    valueType: 'switch',
  },
  {
    title: '字段数',
    dataIndex: 'col',
    key: 'col',
  },
  {
    title: 'Grid显示',
    dataIndex: 'grid',
    key: 'grid',
    valueType: 'switch',
    initialValue: true,
  },
  {
    title: 'Size',
    dataIndex: 'size',
    key: 'size',
    valueType: 'digits',
    initialValue: 40,
  },

  /*{
      title: "status",
      dataIndex: "status",
      valueType: "select",
      attribute: "user_auth_occupational"

    },*/
  {
    title: '操作',
    width: '164px',
    key: 'option',
    valueType: 'option',
    render: (_: any, row: any, index: any, action: any) => [
      <CategoryEdit
        row={row}
        onSuccess={function (): void {
          // throw new Error('Function not implemented.');
          action.current?.reload();
        }}
      />,
      <Button
        key="link"
        type={'primary'}
        danger={true}
        onClick={(a) => {
          editCategoryByDelete(row.id)
            .then((res) => {
              message.success('删除成功');
              action.reload();
            })
            .catch((e) => {
              message.error('删除失败');
            });
        }}
      >
        删除
      </Button>,
    ],
  },
];

const cloneTbale = () => {
  const actionRef = useRef<ActionType>();
  const [columns, setColumns] = useState(column);

  return (
    <div>
      <MyTable
        search={false}
        actionRef={actionRef}
        toolbar={() => [
          <CategoryEdit
            row={{
              comp_path: '',
              deleted: 0,
              hideChildrenInMenu: false,
              hideInMenu: false,
              icon: '',
              id: 0,
              name: '',
              permsKey: '',
              routePath: '',
              compPath: '',
              sort: 0,
              status: 0,
              type: 0,
              menuShow: false,
            }}
            onSuccess={function (): void {
              // throw new Error('Function not implemented.');
              actionRef.current?.reload();
            }}
          />,
        ]}
        request={findByDataCategoryTreeList}
        columns={columns}
        rowKey="id"
      ></MyTable>
    </div>
  );
};

export default cloneTbale;
